/*
 * @Author: 260156 260156@gree.com.cn
 * @Date: 2022-08-16 15:57:08
 * @LastEditors: 260156 260156@gree.com.cn
 * @LastEditTime: 2022-10-26 10:09:20
 * @FilePath: \gree-zst-web\src\utils\helper.ts
 * @Description:
 *
 * Copyright (c) 2022 by 260156 260156@gree.com.cn, All Rights Reserved.
 */
import { ElMessageBox } from "element-plus";
import { router } from "@/router";

interface Options {
  title?: string;
  message?: string;
  successCallback?: () => any;
}
export const requireLoginMessageBox = (options: Options = {}) => {
  const { title = "提示", message = "请登录", successCallback } = options;

  ElMessageBox.alert(message, title, {
    type: "warning",
    showClose: false,
  }).then(() => {
    typeof successCallback === "function"
      ? successCallback()
      // : (window.location.href = import.meta.env.VITE_CALLBACK);
      : (router.replace("/login"));
  });
};

export const doScrollToTop = (speedPercentage: number): Promise<void> => {
  let i: any = null;
  return new Promise((resolve, reject) => {
    i = setInterval(() => {
      const scrollTop =
        document.body.scrollTop || document.documentElement.scrollTop;
      const scrollTo =
        scrollTop === Math.round(scrollTop * speedPercentage)
          ? scrollTop - 1
          : Math.round(scrollTop * speedPercentage);
      document.body.scrollTop
        ? (document.body.scrollTop = scrollTo)
        : (document.documentElement.scrollTop = scrollTo);
      if (scrollTo === 0) {
        i !== null && clearInterval(i);
        resolve();
      }
    }, 30);
  });
};
